package com.springboot.demo.design.pattern.form;

import com.springboot.common.entity.request.Parameter;
import com.springboot.common.entity.request.ParameterBody;
import com.springboot.common.entity.response.Result;
import com.springboot.demo.design.pattern.enums.FormEnum;
import java.util.Date;
import lombok.extern.log4j.Log4j2;

/**
 * 标准表单
 *
 * @author supu
 * @date 2019-03-19 14:25
 **/
@Log4j2
public class StardardForm<U, V extends ParameterBody> implements Form<U, V> {
    private static final long serialVersionUID = -4580526518823705597L;

    private final Date submitTime;
    private final String formNo;
    private final FormEnum formEnum;
    private final Parameter<V> parameter;
    private Result<U> result;
    private Throwable throwable;

    public StardardForm(FormEnum formEnum, Parameter<V> parameter) {
        this.formEnum = formEnum;
        this.parameter = parameter;
        this.submitTime = new Date();
        this.formNo = parameter.getSerialId();
    }

    @Override
    public FormEnum getFormEnum() {
        return formEnum;
    }

    @Override
    public String getFormNo() {
        return formNo;
    }

    @Override
    public Date getSubmitTime() {
        return submitTime;
    }

    @Override
    public Parameter<V> getParameter() {
        return parameter;
    }

    @Override
    public Result<U> getResult() {
        return result;
    }

    @Override
    public void setResult(Result<U> result) {
        this.result = result;
    }

    @Override
    public Throwable getThrowable() {
        return throwable;
    }

    @Override
    public void setThrowable(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public boolean validate() {
        if (formEnum == null) {
            log.error("loaderTypeEnum can not be null");
            return false;
        }
        if (parameter == null) {
            log.error("parameter can not be null");
            return false;
        }
        if (!parameter.validate()) {
            log.error("parameter illegal");
            return false;
        }

        return true;
    }
}
